package com.xindaoapp.happypet.fragments;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.LoginManager;

/**
 * 一键绑定
 * 
 * @author chenghao
 * 
 */
public class BindUserFragment extends BaseFragment implements OnClickListener {
    private EditText vUserName;
    private EditText vPassword;
    private View vBind;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_bind_user, container, false);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
        vUserName.addTextChangedListener(mTextWatcher);
        vPassword.addTextChangedListener(mTextWatcher);
        vBind.setOnClickListener(this);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vUserName = (EditText) view.findViewById(R.id.username);
        vPassword = (EditText) view.findViewById(R.id.password);
        vBind = view.findViewById(R.id.bind_account);
    }

    private final TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!TextUtils.isEmpty(vUserName.getText().toString())
                    && !TextUtils.isEmpty(vPassword.getText().toString())) {
                vBind.setEnabled(true);
            } else {
                vBind.setEnabled(false);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void afterTextChanged(Editable s) {}
    };

    @Override
    protected String getTitleName() {
        return "绑定帐号";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bind_account:
                if (checkInput()) {
                    if (getArguments().getInt(Constants.KEY_LOGIN_TYPE) == Constants.LOGINMODE.QQ
                            .ordinal()) {
                        new LoginManager(mActivity).bindQQ(vUserName.getText().toString(),
                                vPassword.getText().toString());
                    } else {
                        new LoginManager(mActivity).bindSina(vUserName.getText().toString(),
                                vPassword.getText().toString());
                    }
                }
                break;

            default:
                break;
        }
    }

    private boolean checkInput() {
        if (TextUtils.isEmpty(vUserName.getText().toString())) {
            Toast.makeText(mActivity, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(vPassword.getText().toString())) {
            Toast.makeText(mActivity, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
